package com.qiyeji.real_estate.controller;


import com.qiyeji.real_estate.entity.staff;
import com.qiyeji.real_estate.service.StaffService;
import com.qiyeji.real_estate.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/Staff")
@RestController
public class StaffController {

    @Autowired
    StaffService staffService;

    @PostMapping("/All_Staff")
    public Result getAllStaff() {
        List<staff> Staff = staffService.getAllStaff();

        if (Staff == null) {
            return Result.error();
        }
        else
            return Result.success(Staff);
    }

    @PostMapping("/Add_Staff")
    public Result addStaff(@RequestBody Map<String, String> params) {
        String sCode = params.get("sCode");
        String sName = params.get("sName");
        String dCode = params.get("dCode");
        String job = params.get("job");
        String sTel = params.get("sTel");
        String sState = params.get("sState");
        staff record = new staff(sCode, dCode, sName, job, sTel, sState);
        Integer res = staffService.addStaff(record);

        if (res == null) {
            return Result.error();
        }
        else {
            return Result.success(res);
        }
    }

    @PostMapping("Delete_Staff")
    public Result deleteStaff(@RequestBody Map<String, String> params) {
        String sCode = params.get("sCode");
        Integer res = staffService.deleteStaff(sCode);

        if (res == null) {
            return Result.error();
        }
        else {
            return Result.success(res);
        }
    }

    @PostMapping("Alter_Staff")
    public Result alterStaff(@RequestBody Map<String, String> params) {
        Integer res = staffService.alterStaff(new staff(
                params.get("sCode"),
                params.get("dCode"),
                params.get("sName"),
                params.get("job"),
                params.get("sTel"),
                params.get("sState")
        ));
        if (res == null) {
            return Result.error();
        }
        else
            return Result.success(res);
    }

    @PostMapping("Query_Staff")
    public Result queryStaff(@RequestBody Map<String, String> params) {
        staff res = staffService.queryStaff(params.get("sCode"));

        if (res == null) {
            return Result.error();
        }
        else
            return Result.success(res);
    }

    @PostMapping("GetCharges")
    public Result getCharges() {
        List<List<staff>> Charges = new ArrayList<>();
        List<staff> modelCharges = staffService.getChargesByDCode("D001");
        List<staff> renderCharges = staffService.getChargesByDCode("D002");
        List<staff> lateCharges = staffService.getChargesByDCode("D003");

        Charges.add(modelCharges);
        Charges.add(renderCharges);
        Charges.add(lateCharges);

        if (Charges.isEmpty())
            return Result.error();
        else
            return Result.success(Charges);
    }

    @PostMapping("login")
    public Result login(@RequestBody Map<String, String> params){
        String id=params.get("username");
        String password=params.get("password");
        staff staff=staffService.login(id,password);
        if (staff==null){
            return Result.error();
        }else{
            return Result.success(staff);
        }
    }

    @PostMapping("/GetStaffBydCode")
    public Result getStaffBydCode(@RequestBody Map<String, String> params) {
        List<staff> res = staffService.getStaffBydCode(params.get("dCode"));

        if (res == null) {
            return Result.error("该部门没有员工");
        }
        else
            return Result.success(res);
    }

}
